﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using Mapbox.Unity.Map;

public class UIManager : MonoBehaviour
{
    private Vector3 mouseDownPos;
    private Transform tip;
    private Vector3 clickPos;
    private GameObject selectPointItem;
    private bool isGuiding;
    private Guide guide;
    private Transform RouteReplayButton;

    // Use this for initialization
    void Start()
    {
        isGuiding = false;
        tip = transform.Find("Tip");
        guide = FindObjectOfType<Guide>();
        AddButtonListener();
        SetTipState(false);
        guide.warningAction = Warning;
        SetStartRealNavigationState(false);
        RouteReplayButton = transform.Find("RouteReplay");
    }

    private void AddButtonListener()
    {
        tip.Find("SetPoint").GetComponent<Button>().onClick.AddListener(() =>
        {
            guide.SetPoint(clickPos);
            SetTipState(false);
        });

        tip.Find("Cancel").GetComponent<Button>().onClick.AddListener(() =>
        {
            guide.CancelSelect();
            SetTipState(false);
        });

        transform.Find("StartGuide").GetComponent<Button>().onClick.AddListener(SetGuideState);

        transform.Find("StartRealNavigation").GetComponent<Button>().onClick.AddListener(FindObjectOfType<RealNavigation>().Begin);

        transform.Find("Start").GetComponent<Button>().onClick.AddListener(() =>
        {
            SetTipState(false);
            DestroyItem();
            selectPointItem = Instantiate(Load("Prefab/SelectPointItem"), transform.Find("Start"));
            selectPointItem.GetComponent<SelectPointItem>().AddButtonListener(() =>
            {
                guide.SelectSelfPoint(Point.Start);
            }, () =>
            {
                guide.SelectMapPoint(Point.Start);
            });
            SetTipText(Point.Start);
        });

        transform.Find("End").GetComponent<Button>().onClick.AddListener(() =>
        {
            SetTipState(false);
            DestroyItem();
            selectPointItem = Instantiate(Load("Prefab/SelectPointItem"), transform.Find("End"));
            selectPointItem.GetComponent<SelectPointItem>().AddButtonListener(() =>
            {
                guide.SelectSelfPoint(Point.End);
            }, () =>
            {
                guide.SelectMapPoint(Point.End);
            });
            SetTipText(Point.End);
        });

        GPS gps = FindObjectOfType<GPS>();
        transform.Find("Self").GetComponent<Button>().onClick.AddListener(gps.BackToSelf);

        transform.Find("RouteReplay").GetComponent<Button>().onClick.AddListener(RouteMap);//FindObjectOfType<SpawnRoute>().Spawn
    }

    private bool isShowMainMap = true;
    private void RouteMap()
    {
        if (isShowMainMap)
        {
            transform.Find("RouteReplay/Text").GetComponent<Text>().text = "返回地图";
            GetComponent<MapManager>().ShowRouteMap();
        }
        else
        {
            transform.Find("RouteReplay/Text").GetComponent<Text>().text = "路线回放";
            GetComponent<MapManager>().CloseRoute();
        }
        isShowMainMap = !isShowMainMap;
        FindObjectOfType<CameraManager>().IsShowMain = isShowMainMap;
    }

    private void SetStartRealNavigationState(bool isShow)
    {
        transform.Find("StartRealNavigation").gameObject.SetActive(isShow);
    }

    private void SetGuideState()
    {
        isGuiding = !isGuiding;
        Text text = transform.Find("StartGuide").GetComponentInChildren<Text>();
        if (isGuiding)
        {
            bool success = guide.StartGuide();
            text.text = "停止导航";
            SetStartRealNavigationState(true);
            if (!success)
            {
                isGuiding = !isGuiding;
                text.text = "开始导航";
                SetStartRealNavigationState(false);
            }
        }
        else
        {
            guide.EndGuide();
            text.text = "开始导航";
            SetStartRealNavigationState(false);
        }
    }

    private void SetTipText(Point point)
    {
        Text text = tip.Find("SetPoint").GetComponentInChildren<Text>();
        switch (point)
        {
            case Point.Start:
                text.text = "设为起点";
                break;
            case Point.End:
                text.text = "设为终点";
                break;
        }
    }

    private void DestroyItem()
    {
        if (selectPointItem != null)
        {
            Destroy(selectPointItem);
            selectPointItem = null;
        }
    }

    private GameObject Load(string path)
    {
        return Resources.Load(path) as GameObject;
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0) && EventSystem.current.currentSelectedGameObject == null)
        {
            SetTipState(false);
            mouseDownPos = Input.mousePosition;
        }

        if (Input.GetMouseButtonUp(0)
            && mouseDownPos == Input.mousePosition
            && EventSystem.current.currentSelectedGameObject == null)
        {
            SetTipState(true);
            tip.position = Input.mousePosition;

            SendRay();
        }

        //if (RouteModel.Instance.RoutePointCache.Count > 3)
        //{
        //    RouteReplayButton.gameObject.SetActive(true);
        //}
        //else
        //{
        //    RouteReplayButton.gameObject.SetActive(false);
        //}
    }

    private void SendRay()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;

        if (Physics.Raycast(ray, out hitInfo))
        {
            if (hitInfo.collider.gameObject.layer == 8)//Ground
            {
                clickPos = hitInfo.point;
                if(FindObjectOfType<SelectPointItem>() != null)
                {
                    Destroy(FindObjectOfType<SelectPointItem>().gameObject);
                }
            }
        }
    }

    private void SetTipState(bool show)
    {
        if (guide.SelectingState == SelectingState.None)
        {
            tip.gameObject.SetActive(false);
        }
        else
        {
            tip.gameObject.SetActive(show);
        }

        
    }

    private void Warning(string content)
    {
        transform.GetComponentInChildren<Warning>().SetContent(content);
    }
}
